<?php if ( ! defined('IN_DiliCMS')) exit('No direct script access allowed');

    /**
     * 需要在application/config/下建立platform.php配置
     * $running_platform['type'] = 'default';//当前支持传统(default)和新浪云平台(sae).
     * $running_platform['storage'] = '';//用于设置文件存储服务的domain
     */
    class Platform {
    
        /**
         * 支持平台，现在只有 sina
         * 
         * @var array 
         */
        private $_supported_platforms  = array('default','sae');
        
        /**
         * 平台
         * 
         * @var object 
         */
        private $_platform = NULL;
        
        /**
         * 构造函数
         */
        public function __construct() { $this->_init(); }
        
        private function _init() {
            $platform = '';
            if(file_exists(APPPATH.'config/platform'.EXT)) {
               include APPPATH.'config/platform'.EXT;
               if(isset($running_platform['type']) && $running_platform['type']) {
                   $platform = $running_platform['type'];
               }
            }
            if(!in_array($platform,$this->_supported_platforms)) {
                $platform = $this->_supported_platforms[0];
            }
            $platform_class = 'platform_'.$platform;
            if(file_exists($platform_class_path = APPPATH.'libraries/dili/platforms/platform_'.$platform.EXT)) {
                include $platform_class_path;
                if(class_exists($platform_class)) {
                    $this->_platform = new $platform_class($running_platform);
                }
                else {
                    show_error('Platform Class not Found:'.$platform_class);
                }
            }
            else {
                show_error('File Not Found:'.$platform_class);
            }
        }
        
        /**
         * 文件是否存在
         * 
         * @param string $path 路径
         * @return bool 
         */
        public function file_exists($path =  '') { return $this->_platform->file_exists($path); }
        
        /**
         * 写文件
         * 
         * @param string $path 路径
         * @param string $content 内容
         * @return bool 
         */
        public function file_write($path = '',$content = '') { return $this->_platform->file_write($path,$content); }
        
        /**
         * 读取文件内容
         * 
         * @param string $path 路径
         * @return string 文件内容 
         */
        public function file_read($path = '') { return $this->_platform->file_read($path); }
        
        /**
         * 删除文件
         * 
         * @param string $path 文件路径
         * @return bool 
         */
        public function file_delete($path = '') { return $this->_platform->file_delete($path); }
        
        /**
         * 上传文件
         * 
         * @param string $from 来源路径
         * @param string $to 要上传到
         * @return bool 
         */
        public function file_upload($from = '' , $to = '') { return $this->_platform->file_upload($from , $to );   }
		
        /**
         * 文件URL
         * 
         * @param string $path 路径
         * @return string
         */
		public function file_url($path = '') { return $this->_platform->file_url($path);  }
        
        /**
         * 缓存文件是否存在
         * 
         * @param type $path
         * @return type 
         */
        public function cache_exists($path =  '') { return $this->_platform->cache_exists($path); }
        
        /**
         * 写缓存文件
         * 
         * @param string $path 路径
         * @param string $content 内容
         * @return bool 
         */
        public function cache_write($path = '',$content = '') { return $this->_platform->cache_write($path,$content); }
        
        /**
         * 读取缓存
         * 
         * @param string $path 路径
         * @return string 内容 
         */
        public function cache_read($path = '') { return $this->_platform->cache_read($path); }
        
        /**
         * 删除缓存
         * 
         * @param string $path 路径
         * @return bool 
         */
        public function cache_delete($path = '') { return $this->_platform->cache_delete($path); }
        
        /**
         * 取得当前 flatform 的名称
         * 
         * @return type 
         */
        public function get_name() { return $this->_platform->get_name(); }
        
        /**
         * 取得当前 platform 的类型
         * 
         * @return string 
         */
        public function get_type() { return $this->_platform->get_type(); }
    }